package com.arron.jvm.ref;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @Classname RefenceQueueDemo
 * @Date 2020/8/24 0:32
 * @Created by 刘
 */
public class RefenceQueueDemo {

    /**
     * 引用队列：在对象被回收之前，放在引用队列中，用于在回收之前对对象做最后的处理
     * @param args
     */
    public static void main(String[] args) {

        Object o1 = new Object();
        ReferenceQueue<Object> queue = new ReferenceQueue<>();
        WeakReference<Object> objectWeakReference = new WeakReference<>(o1, queue);

        o1 = null;

        System.out.println("===========GC之前============");
        System.out.println(o1);
        System.out.println(objectWeakReference.get());
        System.out.println(queue.poll());

        System.out.println("===========GC之后============");
        System.gc();

        System.out.println(o1);
        System.out.println(objectWeakReference.get());
        System.out.println(queue.poll());


    }


}
